<script setup>
import { onMounted, ref } from 'vue'
import { getCategoryFilterAPI, getSubCategoryAPI } from '@/apis/category'
import { useRoute } from 'vue-router'
import GoodsItem from '../Home/components/GoodsItem.vue'

const route = useRoute()
// 定义一个数据 存储面包屑数据
const filterData = ref({})
// 获取面包屑数据
const getFilterData = async () => {
  const res = await getCategoryFilterAPI(route.params.id)
  filterData.value = res.result
}
onMounted(()=>getFilterData()) 

// 一 获取基础列表渲染
const goodList = ref([])
// 请求时携带的参数
const reqData = ref({
  categoryId: route.params.id,
  page:1,
  pageSize:20,
  sortField:'publishTime'
})
// 二  获取列表数据
const getGoodList = async () => {
  const res = await getSubCategoryAPI(reqData.value)
  // console.log(res)
  // console.log(reqData.value.categoryId)
  goodList.value = res.result.items
}
onMounted(()=> getGoodList())

// 三 tab 切换时触发
const tabChange = () => {
  // console.log('tab切换了', reqData.value.sortField)
  reqData.value.page = 1
  getGoodList()
}
// 四 无限加载
const disabled = ref(false)
const load = async () => {
  // console.log('到底部啦!')
  // 获取下一页数据
  reqData.value.page++
  // 这里不直接调用 getGoodList 的原因是 需要拿到 res, 拿到新数据 和旧数据做拼接
  const res = await getSubCategoryAPI(reqData.value)
  goodList.value = [...goodList.value, ...res.result.items]
  // 当 items 为空, 表示没有数据了, 停止监听
  if(res.result.items.length === 0) {
    disabled.value = true
  }
}
</script>

<template>
  <div class="container ">
    <!-- 面包屑 -->
    <div class="bread-container">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <!-- 一级分类的显示 -->
        <el-breadcrumb-item :to="{ path: `/category/${filterData.parentId}` }">{{ filterData.parentName }}
        </el-breadcrumb-item>
        <!-- 二级分类的显示 -->
        <el-breadcrumb-item>{{ filterData.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="sub-container">
      <!-- v-model 的作用的是, 当点击某一项时, 就把这项的 name 属性给 v-model, 也就是给 reqData里面的 sortField-->
      <!-- tab-change 属性是 当 tab 切换时触发 -->
      <el-tabs v-model="reqData.sortField" @tab-change="tabChange">
        <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
        <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
        <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
      </el-tabs>
      <!-- v-infinite-scroll 滚动到底部时, 加载更多数据 -->
      <!-- :infinite-scroll-disabled 属性的属性值 是一个布尔值, 表示是否停止监听 , true为停止监听 -->
      <div class="body" v-infinite-scroll="load" :infinite-scroll-disabled="disabled">
        <!-- 商品列表-->
         <GoodsItem v-for="item in goodList" :goods="item" :key="item.id" />
      </div>
    </div>
  </div>

</template>



<style lang="scss" scoped>
.bread-container {
  padding: 25px 0;
  color: #666;
}

.sub-container {
  padding: 20px 10px;
  background-color: #fff;

  .body {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
  }

  .goods-item {
    display: block;
    width: 220px;
    margin-right: 20px;
    padding: 20px 30px;
    text-align: center;

    img {
      width: 160px;
      height: 160px;
    }

    p {
      padding-top: 10px;
    }

    .name {
      font-size: 16px;
    }

    .desc {
      color: #999;
      height: 29px;
    }

    .price {
      color: $priceColor;
      font-size: 20px;
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }


}
</style>